<template>
  <div class="login_container">
    <div class="login_box">
      <!--标题区-->
      <div class="avatar_box">
        <h3>报告管理系统登陆界面</h3>
      </div>
      <!--登陆表单区-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-view" type="password"></el-input>
        </el-form-item>
        <!--用户类型-->
        <el-form-item class="login_radio">
          <el-radio v-model="loginForm.usertype" label="3">管理员</el-radio>
          <el-radio v-model="loginForm.usertype" label="1">教师</el-radio>
          <el-radio v-model="loginForm.usertype" label="2">学生</el-radio>
        </el-form-item>
        <!--按钮-->
        <el-form-item class="btns">
          <el-button type="primary" size="small" @click="login">登录</el-button>
          <el-button type="info" size="small" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: '',
        usertype: ''
      },
      // 规则校验
      loginFormRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }

    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      // console.log(this.loginForm.radio)
      // if (this.loginForm.usertype === '3') {
      //   this.$message.success('登陆成功')
      //   this.$router.push('/admin')
      // } else if (this.loginForm.usertype === '1') {
      //   this.$message.success('登陆成功')
      //   this.$router.push('/teach')
      // } else if (this.loginForm.usertype === '2') {
      //   this.$message.success('登陆成功')
      //   this.$router.push('/stu')
      // } else {
      //   this.$message.error('请选择用户类型')
      // }
      const type = this.loginForm.usertype
      if (type !== '') {
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('/login', this.loginForm)
          console.log(res.meta.msg)
          console.log(type)
          console.log(res.meta.status === '200')
          if (res.meta.status !== '200') {
            return this.$message.error(res.meta.msg)
          }
          // 存token
          window.sessionStorage.setItem('token', res.data.token)
          let path = ''
          if (type === '3') {
            path = '/admin'
          } else if (type === '1') {
            path = '/teach'
          } else if (type === '2') {
            path = '/stu'
          }
          console.log(path)
          this.$message.success(res.meta.msg)
          await this.$router.push(path)
        })
      } else {
        this.$message.error('请选择用户类型')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2c3e50;
  height: 100%;
}

.login_box {
  width: 480px;
  height: 330px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    margin: 22px;
  }

  .login_form {
    position: absolute;
    width: 100%;
    bottom: 0;
    padding: 0 10%;
    box-sizing: border-box;
  }

  .login_radio {
    display: flex;
    justify-content: center;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
